#include "pbc.h"
#include "const.h"
#include "xyz.h"

namespace kuai {

	PBC::PBC() {
		ax = by = cz = LARGE_NUMBER;
		bx = cx = cy = 0;
	}

	PBC::PBC(const RealNumber& a, const RealNumber& b, const RealNumber& c)
	{
		ax = a;
		by = b;
		cz = c;
		bx = cx = cy = 0;
	}

	PBC::PBC(const RealNumber& a, const RealNumber& b, const RealNumber& c, const RealNumber& alpha, const RealNumber& beta, const RealNumber& gamma)
	{
		RealNumber cosA = cos(alpha), cosB = cos(beta), cosG = cos(gamma);
		RealNumber sinG = sin(gamma);
		ax = a;

		bx = b * cosG;
		by = b * sinG;

		cx = c * cosB;
		cy = c * (cosA - cosB*cosG) / sinG;
		cz = sqrt(c*c - cx*cx - cy*cy);
	}

	void PBC::update(XYZ& v) const {
		while (v.z < -cz/2) {
			v.x += cx;
			v.y += cy;
			v.z += cz;
		}
		while (v.z > cz/2) {
			v.x -= cx;
			v.y -= cy;
			v.z -= cz;
		}
		while (v.y < -by/2) {
			v.x += bx;
			v.y += by;
		}
		while (v.y > by/2) {
			v.x -= bx;
			v.y -= by;
		}
		while (v.x < -ax/2) {
			v.x += ax;
		}
		while (v.x > ax/2) {
			v.x -= ax;
		}
	}
}


